package com.ebt.m.proposal_v2.widget.common;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageButton;
import android.widget.TextView;

import com.ebt.m.R;
import com.ebt.m.proposal_v2.interfaces.OnRxClickListener;
import com.ebt.m.proposal_v2.utils.RxUtils;
import com.ebt.m.proposal_v2.widget.base.CompatLinearLayout;

import butterknife.BindView;

/**
 * @author archer.qi
 *         Created on 2017/5/12.
 */
public class TopBar extends CompatLinearLayout {

    @BindView(R.id.leftBar)
    ImageButton leftBar;
    @BindView(R.id.rightBar)
    ImageButton rightBar;
    @BindView(R.id.title)
    TextView title;

    private Activity bindActivity;
    private OnClickListener leftClickListener;

    public TopBar(Context context) {
        super(context);
    }

    public TopBar(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected int setContentView() {
        return R.layout.layout_top_bar;
    }

    @Override
    protected void onBindView() {
        leftBar.setOnClickListener(v -> {
            if (leftClickListener != null) {
                leftClickListener.onClick(leftBar);
            }else {
                if (bindActivity != null) {
                    bindActivity.finish();
                }
            }
        });
    }

    public void with(Activity activity) {
        bindActivity = activity;
    }

    public void setTitle(String title) {
        this.title.setText(title);
    }

    public void setRightClickListener(OnRxClickListener listener) {
        RxUtils.click(rightBar, listener);
    }

    public void setLeftClickListener(OnClickListener listener) {
        leftClickListener = listener;
    }

    public void showLeft(boolean shown) {
        leftBar.setVisibility(shown ? VISIBLE : GONE);
    }

    public void showRight(boolean shown) {
        rightBar.setVisibility(shown ? VISIBLE : GONE);
    }

    public void setRightResource(int id) {
        rightBar.setImageResource(id);
    }
}
